package ngames.com.sushitv.action;

import android.app.Activity;
import android.view.View;

import java.util.ArrayList;

import ngames.com.sushitv.adapter.DemoAdapter;
import ngames.com.sushitv.base.BaseListAction;
import ngames.com.sushitv.core.ModelCallback;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.interfaces.OnItemClickListener;
import ngames.com.sushitv.tools.Constants;

/**
 * Created by Jeffrey on 2016/7/27.
 */
public class DemoListAction extends BaseListAction {
    private String mDataID = "0";
    private DemoAdapter mDemoAdapter;

    public DemoListAction(Activity activity, View baseView) {
        super(activity, baseView);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setListDividerHeight(0xFFE7E7E7, 4);
        onRefreshData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefreshData() {
        mDataID = "0";
        videoRank();
    }

    @Override
    public void loadMore() {
//        videoRank();
    }

    private void videoRank() {
        getAppModel().videoRank(Constants.VIDEO_RANK_TYPE_ALL, Constants.VIDEO_RANK_CYCLE_MONTH, null, mDataID, new ModelCallback<ArrayList<ItemVideo>>() {
            @Override
            public void onBeforeRequest(String urlTag) {

            }

            @Override
            public void onSuccess(ArrayList<ItemVideo> data) {
                mIsLoadingMore = false;
                //mSwipeRefreshLayout.setRefreshing(false);
                if ("0".equals(mDataID) && mDemoAdapter != null) {
                    mDemoAdapter.clearData();
                }
                if (isEmptyList(data)) {
                    return;
                }
                mDataID = data.get(data.size() - 1).getId();
                setListData(data);
            }

            @Override
            public void onFailure(String errorEvent, String message) {
                mIsLoadingMore = false;
                //mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    private void setListData(ArrayList<ItemVideo> data) {
        if (mDemoAdapter == null) {
            mDemoAdapter = new DemoAdapter(mActivity, data);
            mDemoAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                }

                @Override
                public void onItemButtonClick(View view, int position) {

                }
            });
            mRecyclerView.setAdapter(mDemoAdapter);
        } else {
            mDemoAdapter.addData(data);
        }
    }
}
